var pnum = "";  
var initData = function(num){
	    var param = {
			page_num:num,
			page_size:"10"}
		$.ajax({
	        url: base + "/dataCenter/businessdata/findEquipmentBudgetList",
	        data: param,
	        success: function (data, textStatus, jqXHR) {
	        	var pageCount = data.pager.pageCount;
	        	var dataObj ={
	        			"list":data.list,
	         			"dataTool":{
	         				"title":{
	          			      	"equipment_budget_table_code": "设备概算代号"
	          			      	,"equipment_budget_table_name":"设备概算名称"
	          			      	,"project_design_phase_name":"设计阶段名称"
	          			      	,"project_design_major_name":"设计专业名称"
	          			      	,"project_design_content_name":"设计内容名称"
	          			      	,"operation":"操作"
	            			},
	            			"titleCheckbox":false,
							"btn":[{"event":"updateObj","name":"编辑"},
							       {"event":"delObj","name":"删除"},
							       {"event":"updateFormField","name":"编辑表单信息"}]
						}
		  		};
	        	showPageTable('view', dataObj,num,pageCount,"initData");
	        },
	        error: function () {
	        }
	    });
}  

  var delObj = function(obj){
	   layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
		   var id = obj.id,code = obj.equipment_budget_table_code
		   $.ajax({
			   url:base+"/dataCenter/businessdata/delEquipmentBudgetById/"+id+"/"+code,
			   data:{},
			   success:function(data,status){
				    if(data.statusCode == 0){
						layer.msg(data.message, {
							icon : 1
						});
				    }else{
						layer.msg(data.message, {
							icon : 5
						});
				    }
				    initData(pnum);
			   }
		   });
	   });
  }
  
  //编辑表单信息
  var updateFormField = function(obj){
	   var code = "dc_sbgs_"+obj.equipment_budget_table_code;
	   openStructurLay(code);
  }
  
  //打开结构化数据维护弹窗
  var openStructurLay = function(code){
		layer.open({
		  	title:'编辑表单'
			,content:initFormField(code)
	    	,area: ['950px', '800px']
		    ,scrollbar: false
		  	,btn: ['保存', '取消']
			,yes: function(index,layero){ //确认
				commit_field_data(code);
			}
	    });
  }
  
  //取得表单信息
  var initFormField = function(code){
	   var contentDiv;
	   $.ajax({
		   url:base+"/dataCenter/findFromFieldHtml/"+code,
		   data:{},
		   async: false,
		   success:function(data,status){
			   contentDiv = data;
		   }
	   });
	   return contentDiv;
  }
  
  //校验表单数据
  var verify_table_data = function(){
	   var juage = true;
	   var data_obj = $("#from_field_table").children("tr");
	   $.each(data_obj,function(index,value){
		   if(index>1){
			   $.each($(this).children("td"),function(children_index,value){
				   if(children_index > 1){
					   var obj_name = $(this).children().attr("name");
					   var obj_val = $(this).children().val();
					   if("field_type"==obj_name && ("varchar"==obj_val || "int"==obj_val)){
						   var field_length_obj = $(this).parent().find("td input[name='field_length']");
						   if(field_length_obj.val() == "" || field_length_obj.val() == undefined){
							   field_length_obj.focus();
							   layer.tips('字段长度不能为空', field_length_obj);
							   juage = false;
							   return false;
						   }
					   }
					   if("field_name"==obj_name && (""==obj_val || undefined==obj_val)){
						   var field_name_obj = $(this).parent().find("td input[name='field_name']");
						   field_name_obj.focus();
						   layer.tips('字段名不能为空', field_name_obj);
						   juage = false;
						   return false;
					   }
				   }
			   });
			   if(!juage){
				   return false;
			   }
		   }
	   });
	   return juage;
  }
  
  //封装数据
  var commit_field_data = function(code){
	   if(false==verify_table_data()){
		   return false;
	   }
	   var json_array = [];
	   var data_obj = $("#from_field_table").children("tr");
	   $.each(data_obj,function(index,value){
		   if(index > 1){
			   var json_str = {};
			   $.each($(this).children("td"),function(children_index,value){
				   if(children_index > 1){
					   var input_obj = $(this).children();
					   var input_name = $(this).children().attr("name");
					   if(input_name=="field_name"){
						   json_str['field_name']=$.trim(input_obj.val());
					   }else if(input_name=="field_comment"){
						   json_str['field_comment']=$.trim(input_obj.val());
					   }else if(input_name=="field_length"){
						   json_str['field_length']=$.trim(input_obj.val());
					   }else if(input_name=="field_default"){
						   json_str['field_default']=$.trim(input_obj.val());
					   }else if(input_name=="field_type"){
						   json_str['field_type']=$.trim(input_obj.val());
					   }else if(input_name=="is_null"){
						   if(input_obj.is(':checked')){
							   json_str['is_null']='true';
						   }else{
							   json_str['is_null']='false';
						   }
					   }
				   }
			   });
			   json_array.push(json_str);
		   }
	   });
	   var field_list = JSON.stringify(json_array);
	   var table_name = code;
	   var param = {"table_name":table_name,"data_list":field_list};
	   commit_data(param);
  } 
  
  var commit_data = function(param){
	   $.post(base+"/dataCenter/businessdata/updateTableField",param,
		       function(data,status){
			     if(data.statusCode == 0){
				  	layer.msg(data.message, {
						icon : 1
				  	});
			     }else{
					layer.msg(data.message, {
						icon : 5
					});
			    }
		  });
  }
  
  //编辑设备概算信息
  var updateObj = function(obj){
	   var contentDIV = function(){
		   var content;
		   var data = {
				   "title":{
					   "id":"设备概算id",
					   "equipment_budget_table_code":"设备概算表代号",
					   "equipment_budget_table_name":"设备概算表名称"
				   },"text":{
					   "id":obj.id,
					   "equipment_budget_table_code":obj.equipment_budget_table_code,
					   "equipment_budget_table_name":obj.equipment_budget_table_name
				   },"input":{
					   "id":"hidden",
					   "equipment_budget_table_code":"text",
					   "equipment_budget_table_name":"text"
				   },"input_init":{
				   }
		   };
		   var getDiv = dc_update.innerHTML;
		   laytpl(getDiv).render(data,function(html){
			   content = html;
		   });
		   return content;
	   }
	   layopen(contentDIV());
  }
  
  $("#addData").click(function(){
	   var contentDIV = function(){
		   var content;
		   var data = {
				   "title":{
					   "equipment_budget_table_code":"设备概算表代号",
					   "equipment_budget_table_name":"设备概算表名称",
					   "project_design_phase_id":"所属设计阶段",
					   "project_design_major_id":"所属设计专业",
					   "project_design_content_id":"所属设计内容"
				   },"text":{
					   "equipment_budget_table_code":"",
					   "equipment_budget_table_name":"",
					   "project_design_phase_id":"",
					   "project_design_major_id":"",
					   "project_design_content_id":""
				   },"input":{
					   "equipment_budget_table_code":"text",
					   "equipment_budget_table_name":"text",
					   "project_design_phase_id":"select",
					   "project_design_major_id":"new_select",
					   "project_design_content_id":"select"
				   },"input_init":{
				   }
		   };
		   var getDiv = dc_update.innerHTML;
		   laytpl(getDiv).render(data,function(html){
			   content = html;
		   });
		   return content;
	   }
	   layopen(contentDIV());
	   initPhase();
	   initDesignMajor();
	   chanageMajorselect();
  });

  //初始化设计阶段
  var initPhase = function(){
	   $.post(base+"/dataCenter/businessdata/findDesignPhaseData",{},
	   		  	function(data,status){
   		    	if(data != null && data.list != null){
 	      		  	 var optionstring = "";
 	                 $.each(data.list, function(i,item){
 	                	optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_phase_name + "</option>";
 	                });
 	                $("select[name='project_design_phase_id']").html('<option value=""></option>' + optionstring);
 	      		}else{
 	      		  	$("select[name='project_design_phase_id']").html('<option value=""></option>');
 	      		}
   		    form.render(); 
	  });
  }
  
  var initDesignMajor = function(){
	   $.post(base+"/dataCenter/businessdata/findDesignMajorData",{},
  		  	function(data,status){
    		    	if(data!=''){
  	      		  	 var optionstring = "";
  	                 $.each(data.list, function(i,item){
  	                    optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_major_name + "</option>";
  	                });
  	                $("select[name='project_design_major_id']").html('<option value=""></option>' + optionstring);
  	      		}else{
  	      		  	$("select[name='project_design_major_id']").html('<option value=""></option>');
  	      		}
    		    form.render(); 
 });
 }

  //监听专业下拉选
  var chanageMajorselect = function(){
  		form.on("select(new_select)",function(value){
  			var major_id = value.value;
  			initContent(major_id);
  		});
  }
  
  var initContent = function(major_id){
	   $.post(base+"/dataCenter/businessdata/findDesignContent",{major_id:major_id},
	   		  	function(data,status){
	     		    	if(data!=''){
	   	      		  	 var optionstring = "";
	   	                 $.each(data.list, function(i,item){
	   	                    optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_content_name + "</option>";
	   	                });
	   	                $("select[name='project_design_content_id']").html('<option value=""></option>' + optionstring);
	   	      		}else{
	   	      		  	$("select[name='project_design_content_id']").html('<option value=""></option>');
	   	      		}
	     		    form.render(); 
	  });
  }

  var layopen = function(content){
		layer.open({
		  	title:'更新设备概算'
			,content: content
	    	,area: ['650px', '550px']
		    ,scrollbar: false
		  	,btn: ['确认', '取消']
			,yes: function(index,layero){ //确认
		  	    var data = $(layero).find("form").serializeJSON();
		  	    if(verify_data(data)){
					$.post(base+"/dataCenter/businessdata/updateEquipmentBudget",data,
			  		function (data,status){
						if(data!=""&&data!=null){
							if(data.statusCode == 0){
								layer.msg('更新成功', {icon: 1,time:2000});
							}else{
								layer.msg(data.message, {icon: 2,time:2000});
							}
						}else{
							layer.msg('更新失败!', {icon: 2,time:2000});
						}
						initData(pnum);
			  		 })
		  	    }
			}
	});
}

  var verify_data = function(data){
		if(data.equipment_budget_table_code == ""){
			var obj = $("input[name='equipment_budget_table_code']");
			obj.focus();
			layer.tips('请填写设备概算代号', obj);
			return false;
		}
		
		if(data.equipment_budget_table_name == ""){
			var obj = $("input[name='equipment_budget_table_name']");
			obj.focus();
			layer.tips('请填写设备概算名称', obj);
			return false;
		}
		
		if(data.project_design_phase_id == ""){
			alert("设计阶段不能为空!");
			return false;
		}
		
		if(data.project_design_content_id == ""){
			alert("设计内容不能为空!")
			return false;
		}
		
		if(data.project_design_major_id == ""){
			alert("设计专业不能为空!");
			return false;
		}
		return true;
 }









$(document).ready(function(){
	initData(1);
});